package com.handpay.zztong.hp.onlineshop.model.marketorder;
import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.SecureManager;
import com.handpay.framework.fastjson.FastJsonHelper;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.base.model.base.BaseModel;
import com.handpay.zztong.hp.config.HttpUrlConstant;

import java.util.Hashtable;

import se.krka.kahlua.vm.LuaTable;

/**
 * Created by lyang on 2017/3/23.
 */

public class MarketOrderDetailModel extends BaseModel{
    private BusinessEngineListener mBusinessEngineListener;
    private ZZTongActivity mActivity;

    /**
     * 	分销系统订单查询
     * @param activity
     * @param params
     * @param listener
     * @return
     */
    public boolean doMarketOrderDetailPost(ZZTongActivity activity, Hashtable<String, String> params, BusinessEngineListener listener) {
        mBusinessEngineListener = listener;
        mActivity = activity;
        return doPost(activity, HttpUrlConstant. Do_GetMarketOrderDetailByOrderId, params, true, false);
    }

    @Override
    public boolean netResponse(String action, byte[] data) {
        boolean bResult = super.netResponse(action, data);
        if (bResult) {
            return true;
        }
//        mActivity.showAlertDialog(mActivity,mActivity.getString(R.string.tip), mActivity.getString(R.string.update_ModifyCashCard_Success), false);
        if (action.equals(HttpUrlConstant.Do_GetMarketOrderDetailByOrderId)) {
            MarketOrderDetailBean bean = new MarketOrderDetailBean();
            if (data_type == DATA_TYPE.DATA_TYPE_JSON) {
                try {
                    String result = new String(data, "UTF-8");
                    bean = FastJsonHelper.getObject(result, MarketOrderDetailBean.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, true);
                String orderNo = SecureManager.getInstance().des((String) luaTable.rawget("orderNo"), 0, null);
                bean.setOrderNo(orderNo);
                String amount = SecureManager.getInstance().des((String) luaTable.rawget("amount"), 0, null);
                bean.setAmount(amount);
                String transSeq = SecureManager.getInstance().des((String) luaTable.rawget("transSeq"), 0, null);
                bean.setTransSeq(transSeq);
                String goodsName = SecureManager.getInstance().des((String) luaTable.rawget("goodsName"), 0, null);
                bean.setGoodsName(goodsName);
            }
            if (mBusinessEngineListener != null) {
                mBusinessEngineListener.callbackBusinessResponse(bean);
            }
        }
        return true;
    }
}
